using System;
using System.Xml;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace DB4OServer
{
    [Serializable]
    public class DBServerConfig
    {
        private string dbhost;
        private int dbport;
        private string dbuser;
        private string dbpass;
        private string userdbfile;
        private string chardbfile;
        private int worldcount;
        private Hashtable worlds = new Hashtable();

        public string UserDbFile { get { return this.userdbfile; } }
        public string CharDbFile { get { return this.chardbfile; } }
        public string DBHost { get { return this.dbhost; } }
        public int DBPort { get { return this.dbport; } }
        public string DBUser { get { return this.dbuser; } }
        public string DBPass { get { return this.dbpass; } }
        public int Worldcount { get { return this.worldcount; } }
        public Hashtable Worlds { get { return this.worlds; } }

        public DBServerConfig()
        {
            //default values
            userdbfile = "user.db";
            chardbfile = "char.db";
            dbhost = "127.0.0.1";
            dbport = 8000;
            dbuser = "root";
            dbpass = "1q2w3e4r5t";

            XmlTextReader reader=null;
            try
            {
                reader = new XmlTextReader("db.config"); 

                while (reader.Read())
                {
                    XmlNodeType nType = reader.NodeType;
                    if (nType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                            case "dbhost": reader.Read(); this.dbhost = reader.Value; break;
                            case "dbport": reader.Read(); this.dbport = int.Parse(reader.Value); break;
                            case "dbuser": reader.Read(); this.dbuser = reader.Value; break;
                            case "dbpass": reader.Read(); this.dbpass = reader.Value; break;
                            case "userdbfile": reader.Read(); this.userdbfile = reader.Value; break;
                            case "chardbfile": reader.Read(); this.chardbfile = reader.Value; break;
                            case "world": reader.Read(); this.worlds.Add(this.worldcount, reader.Value); this.worldcount++; break;
                        }
                    }
                }
                if (this.worldcount == 0)
                {
                   this.worldcount = 1;
                   this.worlds.Add(0, "LegendEmu");
                }
            }
            catch (Exception)
            {
                this.worldcount = 1;
                this.worlds.Add(0, "LegendEmu");
                SaveConfig();
                return;
            }
            finally
            {
                if(reader != null && reader.ReadState != ReadState.Closed)
                    reader.Close();
            }

        }

        public void SaveConfig()
        {
            XmlTextWriter writer;
            try { writer = new XmlTextWriter("db.config", null); }
            catch (Exception)
            {
                Console.WriteLine("Error: cannot write to config file");
                return;
            }
            writer.WriteStartDocument();
            writer.WriteStartElement("SagaDB");
            writer.WriteStartElement("dbhost");
            writer.WriteString(this.dbhost);
            writer.WriteEndElement();
            writer.WriteStartElement("dbport");
            writer.WriteString(this.dbport.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("dbuser");
            writer.WriteString(this.dbuser.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("dbpass");
            writer.WriteString(this.dbpass.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("userdbfile");
            writer.WriteString(this.userdbfile.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("chardbfile");
            writer.WriteString(this.chardbfile.ToString());
            writer.WriteEndElement();

            foreach (string world in this.worlds.Values)
            {
                writer.WriteStartElement("world");
                writer.WriteString(world);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }
    }
}
